package br.unb.core.manager;

import java.net.UnknownHostException;

import br.unb.core.message.Message;
import br.unb.core.synchronism.Synchronism;
import br.unb.utils.Resource;

/**
 *
 * @author spider
 */
public class SyncManager {

    /*Instance*/
    private static final SyncManager INSTANCE = new SyncManager();

    private SyncManager() {
    }

    static SyncManager getInstance() {
        return INSTANCE;
    }
    static final ManagerSocket MANAGER_SOCKET = ManagerSocket.getInstance();

    /************************* Synchronism ****************************/
    /**
     * (Executed on Manager)
     * Sends a request to the server first and then one to the client to
     * begin the synchronization
     *
     */
    Synchronism handleSync(Synchronism syncModel) {
        Message resp;
        try {
            //Message to the server
            resp = MANAGER_SOCKET.sendControlMessage(
                    new Message(Message.Type.START_SYNCSERVER, syncModel),
                    syncModel.getServerIpAddress(), 3);
            if (Message.verifyError(resp)) {
                return null;
            }

            Synchronism respSyncModel = resp.getSyncModel();
            //message to the client
            resp = MANAGER_SOCKET.sendControlMessage(
                    new Message(Message.Type.START_SYNCCLIENT, syncModel),
                    syncModel.getClientIpAddress(), 3);
            if (Message.verifyError(resp)) {
                return null;
            }
            respSyncModel = resp.getSyncModel();
            return respSyncModel;
        } catch (UnknownHostException e) {
            e.printStackTrace();
            Manager.getInstance().printError(Resource.getString("SERVER_NOT_FOUND"));
            return null;
        }
    }

    /**
     * (Executed on Manager (controller machine))
     * Sends one request to the server and one to the client to stop
     * the synchronization
     */
    Synchronism stopSync(Synchronism syncModel) {

        Message resp;
        try {
            resp = MANAGER_SOCKET.sendControlMessage(
                    new Message(Message.Type.STOP_SYNCCLIENT, syncModel),
                    syncModel.getClientIpAddress(), 3);
            if (Message.verifyError(resp)) {
                return null;
            }

            resp = MANAGER_SOCKET.sendControlMessage(
                    new Message(Message.Type.STOP_SYNCSERVER, syncModel),
                    syncModel.getServerIpAddress(), 3);
            if (Message.verifyError(resp)) {
                return null;
            }
            return syncModel;
        } catch (UnknownHostException e) {
            e.printStackTrace();
            Manager.getInstance().printError(Resource.getString("COULD_NOT_STOP_SERVER"));
            return null;
        }
    }

    /**
     * (Executed on Manager (controller machine))
     * Sends a request to the server to resynchronize
     */
    Synchronism reSync(Synchronism syncModel) {
        Message resp;
        try {
            resp = MANAGER_SOCKET.sendControlMessage(
                    new Message(Message.Type.START_SYNCCLIENT, syncModel),
                    syncModel.getServerIpAddress(), 3);
            if (Message.verifyError(resp)) {
                return null;
            }
            Synchronism respSyncModel = resp.getSyncModel();
            return respSyncModel;
        } catch (UnknownHostException e) {
            e.printStackTrace();
            Manager.getInstance().printError(Resource.getString("THE_SYNCRONISM_SERVER_OR_CLIENT_COULD_NOT_BE_STOPPED"));
            return null;
        }
    }
}
